import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import '../provide/person.dart';
import '../provide/setting.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';

class History extends StatefulWidget {
  final Widget child;

  History({Key key, this.child}) : super(key: key);

  _HistoryState createState() => _HistoryState();
}

class _HistoryState extends State<History> {
  // List cartHistory = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('历史订单'),),
      body: Provide<PersonnalProvider>(
        builder: (context,child,val){
          return FutureBuilder(
            future: val.HistoryCart(context),
            builder: (context,snapshot){
              if(snapshot.hasData){
                
                print('哈哈哈哈哈哈哈哈哈');
                print(snapshot.data);
                if(snapshot.data =='not'){
                  return Center(
                    child: Text('您目前没有历史订单'),
                  );
                }
                var data = json.decode(snapshot.data);
                return ListView.builder(
                  itemCount: data['cart'].length,
                  itemBuilder: (context,index){
                    return getcart(data['cart'][index]);
                  },
                );
              }else {
                return Center(
                  child: Text('正在加载中'),
                );
              }
            },
          );
        },
      )
    );
  }

  // 商品单项
  Widget getcart(item) {
    return Container(
      margin: EdgeInsets.fromLTRB(5.0,2.0,5.0,2.0),
      padding: EdgeInsets.fromLTRB(5.0,10.0,5.0,10.0),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(width:1,color:Colors.black12)
        )
      ),
      child: Row(
        children: <Widget>[
          _cartImage(item),
          _cartGoodsName(item),
          _cartPrice(item)
        ],
      ),
    );
  }

    //商品图片 
  Widget _cartImage(item){
    String image;
    if(item['image']!=null){
      image = item['image'];
    }else {
      image = item['images'];
    }
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3.0),
      decoration: BoxDecoration(
        border: Border.all(width: 1,color:Colors.black12)
      ),
      child: Provide<SettingProvide>(
        builder: (context,child,val){
          return val.noImage?Image.asset('images/noImage.png')
                            :CachedNetworkImage(
                              imageUrl: image,
                              placeholder: (context, url) => Center(
                                child: Text('正在加载',style: TextStyle(color: Colors.white),),
                              ),
                              errorWidget: (context, url, error) => new Icon(Icons.error),
                              fit: BoxFit.contain,
                            );
        },
      ),
    );
  }
  //商品名称
  Widget _cartGoodsName(item){
    String name;
    if(item['name']!=null){
      name = item['name'];
    }else {
      name = item['goodsName'];
    }
    return Container(
      width: ScreenUtil().setWidth(400),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: <Widget>[
          Text(name),
          Text('${item['count']}单')
        ],
      ),
    );
  }

  //商品价格
  Widget _cartPrice(item){
    // String price = (item['price'] * item['count']).toString();
    return Container(
        width:ScreenUtil().setWidth(150) ,
        alignment: Alignment.centerRight,
        child: Column(
          children: <Widget>[
            Text(
              '￥共${item['price']}元',
              style: TextStyle(
                color: Colors.pink
              ),
            ),
          ],
        ),
      );
  }
}